package com.joysuch.wwyt.util;

import java.util.LinkedHashSet;
import java.util.Set;

/**
 * @Author ZhangDong
 * @Date 2020/6/8 14:50
 */
public class PasswordCheckUtil {
    private static Set<Character> specialCharSet; // 特殊字符集合
    private static final int MIN_LENGTH = 6; // 最小长度
    private static final int MAX_LENGTH = 32; // 最大长度

    static {
        specialCharSet = defaultSpecialCharSet();
    }

    /**
     * 密码符合规则，返回true
     */
    public static boolean check(String password) {
        if (password == null || password.length() < MIN_LENGTH || password.length() > MAX_LENGTH) {
            // 长度不符合
            return false;
        }
        for (char ch : password.toCharArray()) {
             if (specialCharSet.contains(ch)) {
                return false;
            }
        }
        return true;
    }

    private static Set<Character> defaultSpecialCharSet() {
        Set<Character> specialChars = new LinkedHashSet<>();
        // 键盘上能找到的符号
        //specialChars.add(Character.valueOf('~'));
        //specialChars.add(Character.valueOf('`'));
        //specialChars.add(Character.valueOf('!'));
        //specialChars.add(Character.valueOf('@'));
        //specialChars.add(Character.valueOf('#'));
        //specialChars.add(Character.valueOf('$'));
        //specialChars.add(Character.valueOf('%'));
        //specialChars.add(Character.valueOf('^'));
        //specialChars.add(Character.valueOf('&'));
        //specialChars.add(Character.valueOf('*'));
        //specialChars.add(Character.valueOf('('));
        //specialChars.add(Character.valueOf(')'));
        //specialChars.add(Character.valueOf('-'));
        //specialChars.add(Character.valueOf('_'));
        //specialChars.add(Character.valueOf('+'));
        //specialChars.add(Character.valueOf('='));
        //specialChars.add(Character.valueOf('{'));
        //specialChars.add(Character.valueOf('['));
        //specialChars.add(Character.valueOf('}'));
        //specialChars.add(Character.valueOf(']'));
        //specialChars.add(Character.valueOf('|'));
        specialChars.add(Character.valueOf('\\'));
        //specialChars.add(Character.valueOf(':'));
        //specialChars.add(Character.valueOf(';'));
        specialChars.add(Character.valueOf('"'));
        specialChars.add(Character.valueOf('\''));
        //specialChars.add(Character.valueOf('<'));
        //specialChars.add(Character.valueOf(','));
        //specialChars.add(Character.valueOf('>'));
        //specialChars.add(Character.valueOf('.'));
        //specialChars.add(Character.valueOf('?'));
        specialChars.add(Character.valueOf('/'));
        return specialChars;
    }
}
